/* 
 * Distributed as part of jdiff
 * 
 * Copyright (C) 2008 QArks.com
 *
 * Author: Pierre Meyer <support@qarks.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1, as 
 * published by the Free Software Foundation.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; see the file LICENSE.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */
package diff.tela;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import com.qarks.util.*;
import diff.misc.*;
import com.qarks.util.ui.*;

public class FileDiffMonitorDialog extends JDialog implements ActionListener{

	private FileDiffResult result = null;
	private DiffThread diffThread;
	private Frame parentFrame;
	private JButton cancel;
	private Timer timer;
	private DiffPanel editor;

	public FileDiffMonitorDialog(Dialog parentDialog, String leftText, String rightText, DiffPanel editor){
		super(parentDialog,"Diff monitor",true);
		init(leftText,rightText,editor);
	}

	public FileDiffMonitorDialog(Frame parentFrame, String leftText, String rightText, DiffPanel editor){
		super(parentFrame,"Diff monitor",true);
		init(leftText,rightText,editor);
	}

	private void init(String leftText, String rightText, DiffPanel editor){
		this.editor = editor;

		timer = new Timer(500,this);
		timer.setRepeats(false);

		diffThread = new DiffThread(leftText,rightText);

		Container cont = getContentPane();
		cont.setLayout(new GridLayout(1,1));

		JPanel mainPanel = new JPanel(new BorderLayout(10,10));
		mainPanel.setBorder(new EmptyBorder(10,10,10,10));

		cancel = new JButton("Cancel");
		cancel.addActionListener(this);
		JPanel cancelPanel = new JPanel(new GridBagLayout());
		cancelPanel.add(cancel, new GridBagConstraints());

		add(mainPanel);
		String message = "<html>Please wait while diff is computed. Cancelling will stop minimum set of difference lookup for a quicker algorithm.</html>";
		mainPanel.add(new JLabel(message),BorderLayout.NORTH);
		mainPanel.add(cancelPanel,BorderLayout.CENTER);

		setSize(300,150);
	}

	public void start(){
		diffThread.start();
		timer.start();
	}

	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == timer){
			if (result == null) {
				if (parentFrame != null && parentFrame.isShowing()) {
					DialogUtilities.centerWindow(this, parentFrame);
				} else {
					DialogUtilities.centerOnScreen(this);
				}
			}
		}
		else if (source == cancel){
			diffThread.cancel();
		}
	}

	private void onDiffEnded(){
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				dispose();
				editor.setContent(result);
			}
		});
	}

	///////////////////////////////////////////

	private class DiffThread extends Thread{

		private Cancellable cancellable;
		private String leftText,rightText;

		public DiffThread(String leftText, String rightText){
			cancellable = new CancellableImpl();
			this.leftText = leftText;
			this.rightText = rightText;
		}

		public void cancel(){
			cancellable.cancel();
		}

		public void run(){
			FileDiffResult temp = Diff.diff(leftText,rightText,cancellable);
			if (!cancellable.isCancelled()){
				result = temp;
				if (FileDiffMonitorDialog.this.isShowing()){
					// avoid a too quick flash
					try{
						Thread.sleep(800);
					}
					catch(Exception ex){}
				}
			}
			else{
				result = Diff.quickDiff(leftText,rightText);
			}
			result = Diff.format(result);
			onDiffEnded();
		}
	}
}
